<template>
  <div class="info-entry">
    <el-dialog :visible.sync="visible">
      <div class="panelWrap info-container">
        <div class="info-title title flex flexac flexjcsb fs14 cfff fwb pl p-r-20">
          <div>电梯信息录入</div>
          <i class="el-icon-close" @click="handleCloseDialog" />
        </div>
        <div class="info-main flex flexdc">
          <div class="info-content flex flexdc flexcenter m-t-40">
            <div class="flex flexac m-b-50">
              <span class="name texttr"><span class="mustval">*</span>位置码：</span>
              <el-input v-model="form.rescueCode" class="input" type="number" placeholder="请输入注册码" />
            </div>
            <div class="flex flexac ">
              <span class="reg-name texttr"><span class="mustval">*</span>注册代码：</span>
              <el-input v-model="form.registerCode" class="input" type="number" placeholder="请输入注册代码" />
            </div>
          </div>
          <div class="info-footer flexcenter">
            <el-button v-if="!isEdit" class="saveBtn m-r-30 el-icon-circle-check" @click="handleSave">保 存</el-button>
            <el-button v-else type="success" class="m-r-30 btn el-icon-circle-check" @click="handleUpdate">更 新</el-button>
            <el-button class="clsoeBtn el-icon-circle-close" @click="handleCloseDialog">关 闭</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import RegexInput from '@/utils/RegexInput'
import { elevatorCreateAdd } from '@/api/elevator/elevator'
import { showNotice } from '@/utils/toolclass'
export default {
  props: {
    value: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      visible: false,
      form: { ...this.data }
    }
  },
  watch: {
    visible(v) {
      this.$emit('input', v)
    },
    value(v) {
      this.visible = v
    },
    data(v) {
      this.form = { ...v }
    }
  },
  methods: {
    handleCloseDialog() {
      this.visible = false
      this.$emit('handleCloseDialog')
      this.$emit('handleRefresh')
    },
    handleSave() {
      if (!(RegexInput.safeCode(this.form.rescueCode))) {
        showNotice('error', '提示', '位置码格式错误')
        return
      }
      if (!(RegexInput.regCode(this.form.registerCode))) {
        showNotice('error', '提示', '注册代码格式错误')
        return
      }
      const params = {
        registerCode: this.form.registerCode,
        rescueCode: this.form.rescueCode
      }
      elevatorCreateAdd(params).then(res => {
        if (res.code === '000000') {
          showNotice('success', '提示', '保存成功!')
          this.$emit('handleRefresh')
          this.$emit('handleCloseDialog')
          this.form = {
            registerCode: '',
            rescueCode: ''
          }
        } else {
          // showNotice('error', '提示', res.msg)
        }
      })
    },
    handleUpdate() {
      console.log(1)
    }
  }
}
</script>

<style lang="scss">
@import '@/styles/public.scss';
.info-entry {
  .info-container {
    width: 600px;
    background: #ffffff;
    min-height: 400px;
      position: relative;
    .info-main {
      .info-content {
        .reg-name {
          margin-left: -16px;
        }
        .input {
          width: 200px;
        }
      }
      .info-footer {
        height: 100px;
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
      }
    }
  }
}

</style>
